package com.qipay.sp.excutor.fuhuipay;

import com.alibaba.fastjson.JSONObject;
import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

@Component
public class FuHuiWapExcutor extends AbstractExcutor {

    private final static String MERCHANT_NO="merchant_no";                          //商户号
    private final static String MERCHANT_REQ_NO="merchant_req_no";                  //请求订单编号
    private final static String ORDER_AMT="order_amt";                              //交易金额,以元为单位。
    private final static String BG_URL="bg_url";                                     //后台通知地址地址
    private final static String BG_SERVER_AREA="bg_server_area";                    //必填否,大陆写CHN或者为空；境外其他
    private final static String RETURN_URL="return_url";                            //必填否,前台通知地址
    private final static String BIZ_CODE="10001";                                   //10001：支付宝WAP 10002：支付宝扫码

    private final static String SUBJECT="subject";                                  //商品名称
    private final static String PAYER_IP="payer_ip";                                //付款人IP
    private final static String IS_SUPPORT_CD="is_support_cd";                      //必填否,是否支持信用卡支付  1，则表示支持；0：表示不支持。
    private final static String SIGN="sign";                                        //签名

    public final static String PAY_KEY="pay_Key";                                  //签名,交易密钥

    private final static String API_URL="http://api.tumikeji.com/api/order/create"; //请求地址 post


    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.ALIPAY_WAP;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put(FuHuiWapExcutor.MERCHANT_REQ_NO, order.getTradeNo());
        apiParams.put(FuHuiWapExcutor.SUBJECT, order.getBody());
        apiParams.put(FuHuiWapExcutor.ORDER_AMT, order.getActualPayAmount());

        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        String subject=getVal(params, FuHuiWapExcutor.SUBJECT).toString();
        String merchantNo=getVal(spconfig, FuHuiWapExcutor.MERCHANT_NO).toString();
        String merchantReqNo=getVal(params, FuHuiWapExcutor.MERCHANT_REQ_NO).toString();
        String bgUrl=getVal(params, FuHuiWapExcutor.BG_URL).toString();
        String payKey=getVal(spconfig, FuHuiWapExcutor.PAY_KEY).toString();
        String bdServerArea = getVal(params, FuHuiWapExcutor.BG_SERVER_AREA).toString();
        //String returnUrl=getVal(params, FuHuiWapExcutor.RETURN_URL).toString();//前端回调地址

        //金额转换成元
        Integer amout= (Integer) getVal(params, FuHuiWapExcutor.ORDER_AMT);
        BigDecimal bdAmout=BigDecimal.valueOf(amout);
        BigDecimal bdYuanAmout=bdAmout.divide(BigDecimal.valueOf(100), 2,RoundingMode.HALF_UP);
        String orderAmt=bdYuanAmout.toString();

        //组装请求参数
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put("merchant_no", merchantNo);
        reqParams.put("biz_code", FuHuiWapExcutor.BIZ_CODE); // 交易类型
        reqParams.put("subject", subject); // 商品名称
        reqParams.put("order_amt", orderAmt); // 交易金额
        reqParams.put("is_support_cd", "0"); // 关闭信用卡支付，降低投诉率，提高成功率
        reqParams.put("merchant_req_no", merchantReqNo); // 请求订单编号
        reqParams.put("bg_url", bgUrl); //回调地址
        reqParams.put("bg_server_area", bdServerArea); //区域，若境外可以传HK
        //reqParams.put("return_url", returnUrl); //前端回调地址

        //生成sign
        String sign = FHSignUtils.createSign(reqParams, payKey);
        reqParams.put("sign", sign); //区域，若境外可以传HK


        HFResult hfResult=new HFResult();

        Map<String, Object> resultMap = new HashMap<>();
        try {
            String result = super.doJsonPost(FuHuiWapExcutor.API_URL,JSONObject.toJSONString(reqParams));
            hfResult=JSONObject.parseObject(result,HFResult.class);
        }catch (Exception e){
            hfResult.setRsp_code("11");
            hfResult.setRsp_msg("支付宝接口异常");
        }
        if(hfResult!=null){
            if(hfResult.isSuccess()){
                resultMap.put("result_code", "0");
                resultMap.put("redirect_url", hfResult.getRsp_pay_url());
            } else {
                logger.info("支付宝当面付接口请求失败："+hfResult.getRsp_msg());
                resultMap.put("result_code", "1");
                resultMap.put("message", hfResult.getRsp_msg());
            }
        }
        return resultMap;
    }

    /**
     * 反回对象
     */
    public static final class HFResult{

        /**
         * 状态码,“00”,成功,其它失败
         */
        private String rsp_code;

        /**
         * 状态描述,如创建成功
         */
        private String rsp_msg;

        /**
         * 商户号,与请求保持一致
         */
        private String rsp_merchant_no;

        /**
         * 请求订单编号,与请求保持一致
         */
        private String rsp_merchant_req_no;

        /**
         * 平台订单编号
         */
        private String rsp_up_order_no;

        /**
         * 创建订单的金额
         */
        private String rsp_order_amt;

        /**
         * 支付路径
         */
        private String rsp_pay_url;

        /**
         * 业务代码
         */
        private String rsp_biz_code;

        /**
         * 订单状态,当code=00，此属性为2，交易处理中
         */
        private String rsp_state;

        /**
         * 签名
         */
        private String sign;

        public boolean isSuccess(){
            if(this.rsp_code=="00"){
                return true;
            }
            return false;
        }

        public String getRsp_code() {
            return rsp_code;
        }

        public void setRsp_code(String rsp_code) {
            this.rsp_code = rsp_code;
        }

        public String getRsp_msg() {
            return rsp_msg;
        }

        public void setRsp_msg(String rsp_msg) {
            this.rsp_msg = rsp_msg;
        }

        public String getRsp_merchant_no() {
            return rsp_merchant_no;
        }

        public void setRsp_merchant_no(String rsp_merchant_no) {
            this.rsp_merchant_no = rsp_merchant_no;
        }

        public String getRsp_merchant_req_no() {
            return rsp_merchant_req_no;
        }

        public void setRsp_merchant_req_no(String rsp_merchant_req_no) {
            this.rsp_merchant_req_no = rsp_merchant_req_no;
        }

        public String getRsp_up_order_no() {
            return rsp_up_order_no;
        }

        public void setRsp_up_order_no(String rsp_up_order_no) {
            this.rsp_up_order_no = rsp_up_order_no;
        }

        public String getRsp_order_amt() {
            return rsp_order_amt;
        }

        public void setRsp_order_amt(String rsp_order_amt) {
            this.rsp_order_amt = rsp_order_amt;
        }

        public String getRsp_pay_url() {
            return rsp_pay_url;
        }

        public void setRsp_pay_url(String rsp_pay_url) {
            this.rsp_pay_url = rsp_pay_url;
        }

        public String getRsp_biz_code() {
            return rsp_biz_code;
        }

        public void setRsp_biz_code(String rsp_biz_code) {
            this.rsp_biz_code = rsp_biz_code;
        }

        public String getRsp_state() {
            return rsp_state;
        }

        public void setRsp_state(String rsp_state) {
            this.rsp_state = rsp_state;
        }

        public String getSign() {
            return sign;
        }

        public void setSign(String sign) {
            this.sign = sign;
        }
    }
}

